//
//  CommonBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class CommonBillView: UIView {

    fileprivate let cellId = "commonBillCell"
    fileprivate let dataArray = [jingdongBill,alipayBilling,socialSecurityDepositDetails,housingProvidentFundDepositDetails]
    fileprivate let images = ["jingdong","pay","social_security","home"]
    fileprivate let bgArray = ["bg_red","bg_blue","bg_green","bg_yellow"]
    
    var collectionView : UICollectionView!
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = .init(top: RATIO_H(maxNum: 40), left: RATIO(maxNum: 20), bottom: RATIO_H(maxNum: 40), right: RATIO(maxNum: 20))
        layout.minimumInteritemSpacing = RATIO(maxNum: 20)
        layout.itemSize = .init(width: (ScreenW-100)/4, height: RATIO_H(maxNum: 150))
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CommonBillCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CommonBillView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? CommonBillCell
        cell?.imageView.image = UIImage.init(named: images[indexPath.row])
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.contentView.layer.contents = UIImage.init(named: bgArray[indexPath.row])?.cgImage
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
    }
    
}
